package com.ssyt.ExaminationSystem.admin.service;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ssyt.ExaminationSystem.admin.dao.IsystemDao;
import com.ssyt.ExaminationSystem.admin.dao.SystemDaoImpl;
import com.ssyt.ExaminationSystem.admin.entity.MenuEntity;
import com.ssyt.ExaminationSystem.admin.entity.PermissionEntity;
import com.ssyt.ExaminationSystem.admin.entity.RoleEntity;
import com.ssyt.ExaminationSystem.student.entity.GradeEntity;

public class SystemServiceImpl implements IsystemService {

	private IsystemDao systemDao = new SystemDaoImpl();

	/**
	 * 初始化所有的角色
	 */
	@Override
	public List<RoleEntity> initRoles() {

		return systemDao.initRoles();
	}

	/**
	 * 定义根据账号获取权限的json的字符串
	 */
	@Override
	public String getPermissionJsonByaccount(String account) {

		List<MenuEntity> menus = new ArrayList<MenuEntity>();

		List<PermissionEntity> list = systemDao.getMenusByAccount(account);
		// System.out.println(JSON.toJSONString(list, true)
		// .replaceAll("permissionId", "id").replaceAll("parentId", "pId")
		// .replaceAll("permissionName", "name"));

		if (list != null && list.size() > 0) {
			for (PermissionEntity permissionEntity : list) {

				// 转成一个json字符串
				String jsonStr = JSON.toJSONString(permissionEntity);

				jsonStr = jsonStr.replace("permissionId", "id")
						.replace("parentId", "pId")
						.replace("permissionName", "name");
				// 把json字符串转换成对象
				MenuEntity menu = JSON.parseObject(jsonStr, MenuEntity.class);

				// 把转换好的对象存储到menus集合对象中
				menus.add(menu);
			}
			// System.out.println(JSON.toJSONString(menus,
			// true).replaceAll("pid",
			// "pId")); // true用来格式化
		}
		return JSON.toJSONString(menus, true).replaceAll("pid", "pId");
	}

	/**
	 * 查询所有的权限
	 */
	@Override
	public String getPermissions() {
		List<MenuEntity> menus = new ArrayList<MenuEntity>();

		List<PermissionEntity> list = systemDao.getPermissions();
		// System.out.println(JSON.toJSONString(list, true)
		// .replaceAll("permissionId", "id").replaceAll("parentId", "pId")
		// .replaceAll("permissionName", "name"));

		if (list != null && list.size() > 0) {
			for (PermissionEntity permissionEntity : list) {

				// 转成一个json字符串
				String jsonStr = JSON.toJSONString(permissionEntity);

				jsonStr = jsonStr.replace("permissionId", "id")
						.replace("parentId", "pId")
						.replace("permissionName", "name");
				// 把json字符串转换成对象
				MenuEntity menu = JSON.parseObject(jsonStr, MenuEntity.class);

				// 把转换好的对象存储到menus集合对象中
				menus.add(menu);
			}
			// System.out.println(JSON.toJSONString(menus,
			// true).replaceAll("pid",
			// "pId")); // true用来格式化
		}
		return JSON.toJSONString(menus, true).replaceAll("pid", "pId");
	}

	/**
	 * 查询当前用户所拥有的权限
	 */
	@Override
	public String getGrantsByRoleId(String roleId) {
		List<MenuEntity> menus = new ArrayList<MenuEntity>();

		List<PermissionEntity> list = systemDao.getGrantsByRoleId(roleId);
		// System.out.println(JSON.toJSONString(list, true)
		// .replaceAll("permissionId", "id").replaceAll("parentId", "pId")
		// .replaceAll("permissionName", "name"));

		if (list != null && list.size() > 0) {
			for (PermissionEntity permissionEntity : list) {

				// 转成一个json字符串
				String jsonStr = JSON.toJSONString(permissionEntity);

				jsonStr = jsonStr.replace("permissionId", "id")
						.replace("parentId", "pId")
						.replace("permissionName", "name");
				// 把json字符串转换成对象
				MenuEntity menu = JSON.parseObject(jsonStr, MenuEntity.class);

				// 把转换好的对象存储到menus集合对象中
				menus.add(menu);
			}
			// System.out.println(JSON.toJSONString(menus,
			// true).replaceAll("pid",
			// "pId")); // true用来格式化
		}
		return JSON.toJSONString(menus, true).replaceAll("pid", "pId");
	}

	/**
	 * 判断是否授权成功
	 */
	@Override
	public boolean isDoGrant(String permissionsId, String roleId) {
		boolean flag = false;
		String[] permissionId = permissionsId.split(",");

		if (!systemDao.isGrant(roleId)) {
			if (permissionId != null && permissionId.length > 0) {
				for (String pid : permissionId) {

					// 开始重新授权
					int row = systemDao.addGrant(pid, roleId);
					flag = row > 0 ? true : false;
				}
			}

		} else {
			// 如果授权就废弃原来的权限，进行重新授权
			if (systemDao.revoke(roleId) > 0) {
				if (permissionId != null && permissionId.length > 0) {
					for (String pid : permissionId) {

						// 开始重新授权
						int row = systemDao.addGrant(pid, roleId);
						flag = row > 0 ? true : false;
					}
				}
			}

		}

		return flag;
	}

	/**
	 * 添加角色
	 */
	@Override
	public int addRole(String roleName) {

		return systemDao.addRole(roleName);
	}

	/**
	 * 删除角色
	 */
	@Override
	public int deleteRole(String roleId) {

		return systemDao.deleteRole(roleId);
	}

	/**
	 * 班级管理
	 */
	@Override
	public List<GradeEntity> classManager() {

		return systemDao.classManager();
	}

	/**
	 * 删除班级
	 */
	@Override
	public int deleteGrade(String gradeId) {

		return systemDao.deleteGrade(gradeId);
	}

	/**
	 * 修改前查询
	 */
	@Override
	public GradeEntity preUpdateGrade(String gradeId) {

		return systemDao.preUpdateGrade(gradeId);
	}

	/**
	 * 修改班级
	 */
	@Override
	public int updateGrade(String gradeId, String gradeName) {

		return systemDao.updateGrade(gradeId, gradeName);
	}

	/**
	 * 查询所有的赋权限
	 */
	@Override
	public List<PermissionEntity> getFatherGrant() {
		return systemDao.getFatherGrant();
	}

	/**
	 * 通过付权限查询其下面的子权限
	 */
	@Override
	public String getSonGrantsById(String fatherId) {
		String sonStr = "";
		List<PermissionEntity> list = systemDao.getSonGrantsById(fatherId);
		for (PermissionEntity permission : list) {
			sonStr += permission.getPermissionId() + ",";
		}
		sonStr = sonStr.substring(0, sonStr.length() - 1);
		return sonStr;
	}

	/**
	 * 增加权限
	 */
	@Override
	public int addPermissions(PermissionEntity per) {
		return systemDao.addPermissions(per);
	}

	/**
	 * 权限管理
	 */
	@Override
	public List<PermissionEntity> getAllGrants() {
		return systemDao.getAllGrants();
	}

	/**
	 * 删除权限
	 */
	@Override
	public int deleteGrant(String permissionId) {
		return systemDao.deleteGrant(permissionId);
	}

	/**
	 * 修改权限之前的查询
	 */
	@Override
	public PermissionEntity preUpdateGrant(String permissionId) {
		return systemDao.preUpdateGrant(permissionId);
	}

	@Override
	public int updateGrant(PermissionEntity permission) {
		return systemDao.updateGrant(permission);
	}
}
